import unittest
import requests

# 创建测试类
class TestIhrmLogin(unittest.TestCase):
    # 添加测试方法-登录成功
    def test001_login_ok(self):
        # 发送post登录请求，指定url，请求头，请求体，获取响应信息并断言
        response = requests.post(url="http://ihrm-java.itheima.net/api/sys/login",
                                 headers={"Content-Type": "application/json"},
                                 json={"mobile": "13800000002", "password": "123456"}
                                 )
        print(response.json())

        # 断言-响应状态码为200
        self.assertEqual(200,response.status_code)
        # 断言success的值为true
        self.assertEqual(True,response.json().get("success"))
        # 断言code的值为10000
        self.assertEqual(10000,response.json().get("code"))
        # 断言message的值为操作成功
        self.assertIn("成功",response.json().get("message"))

    # 添加测试方法-登录失败(手机号未注册)
    def test002_tel_not_exists(self):
        # 发送post登录请求，指定url，请求头，请求体，获取响应信息并断言
        response = requests.post(url="http://ihrm-java.itheima.net/api/sys/login",
                                 headers={"Content-Type": "application/json"},
                                 json={"mobile": "15723423423", "password": "123456"}
                                 )
        print(response.json())

        # 断言-响应状态码为200
        self.assertEqual(200,response.status_code)
        # 断言success的值为False
        self.assertEqual(False,response.json().get("success"))
        # 断言code的值为20001
        self.assertEqual(20001,response.json().get("code"))
        # 断言message的值为用户名或密码错误
        self.assertIn("错误",response.json().get("message"))

        # 添加测试方法-登录失败(密码错误)
    def test003_pwd_err(self):
        # 发送post登录请求，指定url，请求头，请求体，获取响应信息并断言
        response = requests.post(url="http://ihrm-java.itheima.net/api/sys/login",
                                 headers={"Content-Type": "application/json"},
                                 json={"mobile": "13800000002", "password": "12345654321"}
                                 )
        print(response.json())

        # 断言-响应状态码为200
        self.assertEqual(500, response.status_code)
        # 断言success的值为False
        self.assertEqual(False, response.json().get("success"))
        # 断言code的值为20001
        self.assertEqual(9999, response.json().get("code"))
        # 断言message的值为用户名或密码错误
        self.assertIn("错误", response.json().get("message"))